using System;
using System.Windows;
using System.Linq;
using Buddy.Common.Math;
using Buddy.Navigation;
using Buddy.Common.Plugins;
using Buddy.BehaviorTree;
using Buddy.CommonBot;
using Buddy.Common;
using Buddy.CommonBot.Logic;
using Buddy.Swtor;
using Buddy.Swtor.Objects;
using System.Collections.Generic;

using Action = Buddy.BehaviorTree.Action;

namespace Buddywing.Plugins
{
    public class NodeinatorPlugin : IPlugin
    {
        #region Implementation of IEquatable<IPlugin>

        public bool Equals(IPlugin other)
        {
            return other.Name == Name;
        }

        #endregion

        #region Implementation of IPlugin

        public string Author { get { return "Neo93"; } }

        public Version Version { get { return new Version(1,0); } }

        public string Name { get { return "Nodeinator"; } }

        public string Description { get { return "Moves to Nodes and opens them"; } }

        public Window DisplayWindow { get { return null; } }

        public TorPlaceable Node { get { return ObjectManager.GetObjects<TorPlaceable>().FirstOrDefault(o => o.Harvestable.CanHarvest); } }
        
        /// <summary> Executes the pulse action. This is called every "tick" of the bot. </summary>
        public void OnPulse()
        {
                                foreach( var o in BuddyTor.Me.KnownAbilitiesContainer)
                {
                   Logging.Write(o.Name + ": " + o.Guid);
                }
            ObjectManager.Update();
            if (BuddyTor.Me.InCombat)
                return;
            Logging.Write("[Nodeinator] Found " + Harvest().Name);
            Harvest().Interact();
            //new Decorator(ret => Node != null,
            //new PrioritySelector(
            //    new Decorator(ret => Node.Distance <= 0.4f,
            //        new Sequence(
            //            new Action(ret => Node.Interact()),
            //                new Action(ret => Logging.Write("[Nodeinator] Looted " + Node.Name)))),
            //    new Decorator(ret => Node.Distance > 0.4f,
            //        new Sequence(
            //            new Action(ret => CommonBehaviors.MoveAndStop(when => Node.Position, 0.4f, true)),
            //                new Action(ret => Logging.Write("[Nodeinator] Move to " + Node.Name + " ( " + Node.Position + " )"))))));
        }
        public static TorPlaceable Harvest()
        {
            TorPlaceable closest = null;
            double closestDist = double.MaxValue;
            ObjectManager.Update();
            foreach (TorPlaceable o in ObjectManager.GetObjects<TorPlaceable>().Where(o => o.Harvestable.CanHarvest))
            {
                if (!o.IsUsable)
                    continue;
                double dist = o.DistanceSqr;
                if (dist < closestDist && o.InLineOfSight)
                {
                    closestDist = dist;
                    closest = o;
                }
            }

            return closest;
        }
        /// <summary> Executes the initialize action. This is called at initial bot startup. (When the bot itself is started, not when Start() is called) </summary>
        public void OnInitialize()
        {
            Logging.Write("[Nodeinator] Let's earn some $$$");
            
        }

        /// <summary> Executes the shutdown action. This is called when the bot is shutting down. (Not when Stop() is called) </summary>
        public void OnShutdown()
        {
        }

        /// <summary> Executes the enabled action. This is called when the user has enabled this specific plugin via the GUI. </summary>
        public void OnEnabled()
        {
        }

        /// <summary> Executes the disabled action. This is called whent he user has disabled this specific plugin via the GUI. </summary>
        public void OnDisabled()
        {
            //TreeHooks.Instance.RemoveHook("QuestProfileBehavior_Hook", 0);
        }

        #endregion
    }
}